//
//  LandmarkDetail.swift
//  ChinaLandmarkDemo
//
//  Created by liy on 2025/2/10.
//

import SwiftUI

struct LandmarkDetail: View {
  let landmark: Landmark
  @EnvironmentObject var userData: UserData
  var landmarkIndex: Int {
    userData.userLandmarks.firstIndex {
      landmark.id == $0.id
    }!
  }

  var body: some View {
    VStack {
      MapView(center: landmark.locationCoordinate)
        .ignoresSafeArea(edges: .top)
        .frame(height: 350)

      Image(landmark.image)
        .resizable()
        .frame(width: 250, height: 250)
        .clipShape(.circle)
        .overlay {
          Circle().stroke(lineWidth: 4).foregroundStyle(.white)
        }
        .shadow(radius: 10)

      VStack {
        HStack {
          Text(landmark.name)
          Button {
            userData.userLandmarks[landmarkIndex].isFavorite.toggle()
          } label: {
            if userData.userLandmarks[landmarkIndex].isFavorite {
              Image(systemName: "star.fill")
                .foregroundStyle(.yellow)
            } else {
              Image(systemName: "star")
                .foregroundStyle(.gray)
            }
          }
          Spacer()
        }
        HStack {
          Text(landmark.city)
          Spacer()
          Text(landmark.province)
        }
      }
      .padding()

      Spacer()
    }
    .navigationBarTitle(Text(landmark.name), displayMode: .inline)
  }
}

#Preview {
  NavigationView {
    LandmarkDetail(landmark: landmarks[1])
  }.environmentObject(UserData())
}
